/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ShoppingCartView.java
 *
 * Created on Apr 17, 2010, 12:59:01 AM
 */
package ticketmaster.presentation.customer.purchase;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.JTable;
import ticketmaster.businesslogic.BookedTicketController;
import ticketmaster.businessobject.ShoppingCart;
import ticketmaster.businessobject.Ticket;
import ticketmaster.dataobject.BookedTicket;
import ticketmaster.dataobject.EventView;
import ticketmaster.presentation.customer.CustomerBasePanel;
import ticketmaster.util.DateTimeHelper;

/**
 *
 * @author Wei Liu
 */
public class ShoppingCartView extends CustomerBasePanel
{

    /** Creates new form ShoppingCartView */
    public ShoppingCartView(ShoppingCart cart, EventView event)
    {
        this.setCart(cart);
        this.setEvent(event);
        initComponents();
        initTable();
    }
    private ShoppingCart cart;
    private EventView event;
    private ShoppingCartTable table;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        btnMore = new javax.swing.JButton();
        btnCheckOut = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ticketmaster.TicketMasterApp.class).getContext().getResourceMap(ShoppingCartView.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 25, 240, 30));

        btnMore.setFont(resourceMap.getFont("btnMore.font")); // NOI18N
        btnMore.setText(resourceMap.getString("btnMore.text")); // NOI18N
        btnMore.setName("btnMore"); // NOI18N
        btnMore.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMoreActionPerformed(evt);
            }
        });
        add(btnMore, new org.netbeans.lib.awtextra.AbsoluteConstraints(660, 100, 130, 70));

        btnCheckOut.setFont(resourceMap.getFont("btnCheckOut.font")); // NOI18N
        btnCheckOut.setText(resourceMap.getString("btnCheckOut.text")); // NOI18N
        btnCheckOut.setName("btnCheckOut"); // NOI18N
        btnCheckOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCheckOutActionPerformed(evt);
            }
        });
        add(btnCheckOut, new org.netbeans.lib.awtextra.AbsoluteConstraints(660, 190, 130, 70));
    }// </editor-fold>//GEN-END:initComponents

    private void btnMoreActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnMoreActionPerformed
    {//GEN-HEADEREND:event_btnMoreActionPerformed
        this.getPnlParent().removeAll();
        //SeatSelectionView ssv = new SeatSelectionView(this.getEvent());
        SeatsSelectionView ssv = new SeatsSelectionView(this.getEvent());
        ssv.setPnlParent(this.getPnlParent());
        ssv.setCart(this.getCart());
        this.getPnlParent().add(ssv);
        this.getPnlParent().validate();
        this.getPnlParent().repaint();
    }//GEN-LAST:event_btnMoreActionPerformed

    private void btnCheckOutActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCheckOutActionPerformed
    {//GEN-HEADEREND:event_btnCheckOutActionPerformed
        JTable dataTable = table.getTable();
        Timestamp currentTime = DateTimeHelper.getCurrentDateTime();
        Collection<BookedTicket> bookedTickets = new ArrayList<BookedTicket>();
        for (int i = 0; i < dataTable.getRowCount(); i++)
        {
            if (Boolean.parseBoolean(dataTable.getValueAt(i, 4).toString()))
            {
                BookedTicket bookedTicket = new BookedTicket();
                Ticket ticket = (Ticket) dataTable.getValueAt(i, 0);

                bookedTicket.setBookedDate(currentTime);
                bookedTicket.setCustomerID(ticket.getCustomerID());
                bookedTicket.setEventID(ticket.getTicketRate().getEventID());
                bookedTicket.setIsCancelled(false);
                bookedTicket.setIsPaid(false);
                bookedTicket.setSeatCategoryID(ticket.getTicketRate().getSeatCategoryID());
                bookedTicket.setSeatNumber(ticket.getSeat().getRealNumber());
                bookedTicket.setVenueID(ticket.getTicketRate().getVenueID());

                bookedTickets.add(bookedTicket);
            }
            else
            {
                Ticket ticket = (Ticket) dataTable.getValueAt(i, 0);
                cart.remove(ticket);
            }
        }

        BookedTicketController controller = new BookedTicketController();
        bookedTickets = controller.saveBookedTickets(bookedTickets);
        this.setBookedTickets(bookedTickets);

        directToShippingMethodsView();
    }//GEN-LAST:event_btnCheckOutActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCheckOut;
    private javax.swing.JButton btnMore;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

    private void directToShippingMethodsView()
    {
        ShippingMethodsView smv = new ShippingMethodsView();
        smv.setCart(cart);
        smv.setBookedTickets(this.getBookedTickets());
        smv.setPnlParent(this.getPnlParent());
        this.getPnlParent().removeAll();
        this.getPnlParent().add(smv);
        this.getPnlParent().validate();
        this.getPnlParent().repaint();
    }

    /**
     * @return the cart
     */
    public ShoppingCart getCart()
    {
        return cart;
    }

    /**
     * @param cart the cart to set
     */
    public void setCart(ShoppingCart cart)
    {
        this.cart = cart;
    }

    /**
     * @return the event
     */
    public EventView getEvent()
    {
        return event;
    }

    /**
     * @param event the event to set
     */
    public void setEvent(EventView event)
    {
        this.event = event;
    }

    private void initTable()
    {
        table = new ShoppingCartTable(cart.getTickets());
        add(table, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 95, -1, -1));
    }
}
